

set more off
mat drop _all




use "$path_data/data/temp/attrition_check.dta", clear







qui gen att_any = 0
qui replace att_any = 1 if DT_score_post == . | DT_time_post == . | ///
DT_per_min_post == . | ptsel_overall_score == . | ///
rosen_el == . | cpcs_el == .


qui gen att_any_cog = 0
qui replace att_any_cog = 1 if DT_score_post == . | DT_time_post == . | ///
DT_per_min_post == . | ptsel_overall_score == . 


qui gen att_psc = 0
qui replace att_psc = 1 if Mathematics == .

qui gen att_psc_math_Bangla = 0
qui replace att_psc_math_Bangla = 1 if Mathematics == . | Bangla == .






foreach j in DT_score DT_time DT_per_min pts {
	qui gen att_`j'_post = 0
	qui replace att_`j'_post = 1 if `j'_post == .
	
}
foreach j in rosen cpcs {
	qui gen att_`j'_post = 0
	qui replace att_`j'_post = 1 if `j'_el == .
	
}






*** Table J1


preserve

drop if wrongDT == 1


*foreach j in DT_score DT_time DT_per_min {
foreach j in DT_score  {

*	di ""
*	di ""
*	di ""
*	di ""
*	di ""
*	di "Cluster reg of baseline characteristics with respect to endline attrition status"

	
*	foreach j in DT_score_pre DT_time_pre DT_per_min_pre ptsbl_overall_score ///
*	rosen_bl cpcs_bl {


	foreach s in mean se pv obs {
		matrix Att_`j'_post_`s' = J(1,3,.)
	}
	
	
	
	
	*** Reg of attririon status on treatment dummy
	 reg att_`j'_post treatment if `j'_pre != ., cluster(school_no)

		
	matrix Att_`j'_post_temp = r(table)
	matrix Att_`j'_post_r2 = e(r2)
	matrix Att_`j'_post_obs = e(N)

	 cgmwildboot_2 att_`j'_post treatment if `j'_pre != ., cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix Att_`j'_post_temp_p = e(Pvals)

	
	foreach s in mean be se pv rpv wpv {
		matrix Att_`j'_post_`s' = J(1,2,.)
	}

	foreach i in 1 2 {
		* beta
		matrix Att_`j'_post_mean[1,`i'] = Att_`j'_post_temp[1,`i']
		* standard error
		matrix Att_`j'_post_se[1,`i'] = Att_`j'_post_temp[2,`i']
		* p value
		matrix Att_`j'_post_pv[1,`i'] = Att_`j'_post_temp[4,`i']
		matrix Att_`j'_post_wpv[1,`i'] = Att_`j'_post_temp_p[`i',1]
	}

	
	

	
	
	
		** Reg of baseline-outcome on treatment for attritors
	
*		di ""
*		di ""
*		di ""
*		di "Cluster reg of baseline `j' with respect to the attrition status on endline `j'"
	 reg `j'_pre treatment if att_`j'_post == 1, cluster(school_no)

		
	matrix Att_`j'_pre_temp = r(table)
	matrix Att_`j'_pre_r2 = e(r2)
	matrix Att_`j'_pre_obs = e(N)

	 cgmwildboot_2 `j'_pre treatment if att_`j'_post == 1, cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix Att_`j'_pre_temp_p = e(Pvals)

	
	foreach s in mean be se pv rpv wpv {
		matrix Att_`j'_pre_`s' = J(1,2,.)
	}

	foreach i in 1 2 {
		* beta
		matrix Att_`j'_pre_mean[1,`i'] = Att_`j'_pre_temp[1,`i']
		* standard error
		matrix Att_`j'_pre_se[1,`i'] = Att_`j'_pre_temp[2,`i']
		* p value
		matrix Att_`j'_pre_pv[1,`i'] = Att_`j'_pre_temp[4,`i']
		matrix Att_`j'_pre_wpv[1,`i'] = Att_`j'_pre_temp_p[`i',1]
	}

	
}

restore
	
	
	
	
	
	
	


foreach j in rosen cpcs {

*	di ""
*	di ""
*	di ""
*	di ""
*	di ""
*	di "Cluster reg of baseline characteristics with respect to endline attrition status"

	
*	foreach j in DT_score_pre DT_time_pre DT_per_min_pre ptsbl_overall_score ///
*	rosen_bl cpcs_bl {


	foreach s in mean se pv obs {
		matrix Att_`j'_post_`s' = J(1,3,.)
	}
	
	foreach s in mean se pv obs {
		matrix Att_`j'_pre_`s' = J(1,3,.)
	}
	
	
	
	
	*** Reg of attririon status on treatment dummy
	 reg att_`j'_post treatment if `j'_bl != ., cluster(school_no)

		
	matrix Att_`j'_post_temp = r(table)
	matrix Att_`j'_post_r2 = e(r2)
	matrix Att_`j'_post_obs = e(N)

	 cgmwildboot_2 att_`j'_post treatment if `j'_bl == ., cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix Att_`j'_post_temp_p = e(Pvals)

	
	foreach s in mean be se pv rpv wpv {
		matrix Att_`j'_post_`s' = J(1,2,.)
	}

	foreach i in 1 2 {
		* beta
		matrix Att_`j'_post_mean[1,`i'] = Att_`j'_post_temp[1,`i']
		* standard error
		matrix Att_`j'_post_se[1,`i'] = Att_`j'_post_temp[2,`i']
		* p value
		matrix Att_`j'_post_pv[1,`i'] = Att_`j'_post_temp[4,`i']
		matrix Att_`j'_post_wpv[1,`i'] = Att_`j'_post_temp_p[`i',1]
	}

	
	

	
	
	
		** Reg of baseline-outcome on treatment for attritors
	
*		di ""
*		di ""
*		di ""
*		di "Cluster reg of baseline `j' with respect to the attrition status on endline `j'"
	 reg `j'_pre treatment if att_`j'_post == 1, cluster(school_no)

		
	matrix Att_`j'_pre_temp = r(table)
	matrix Att_`j'_pre_r2 = e(r2)
	matrix Att_`j'_pre_obs = e(N)

	 cgmwildboot_2 `j'_pre treatment if att_`j'_post == 1, cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix Att_`j'_pre_temp_p = e(Pvals)

	
	foreach s in mean be se pv rpv wpv {
		matrix Att_`j'_pre_`s' = J(1,2,.)
	}

	foreach i in 1 2 {
		* beta
		matrix Att_`j'_pre_mean[1,`i'] = Att_`j'_pre_temp[1,`i']
		* standard error
		matrix Att_`j'_pre_se[1,`i'] = Att_`j'_pre_temp[2,`i']
		* p value
		matrix Att_`j'_pre_pv[1,`i'] = Att_`j'_pre_temp[4,`i']
		matrix Att_`j'_pre_wpv[1,`i'] = Att_`j'_pre_temp_p[`i',1]
	}

	
}





*** Panel A and B for PTS


foreach j in pts {

*	di ""
*	di ""
*	di ""
*	di ""
*	di ""
*	di "Cluster reg of baseline characteristics with respect to endline attrition status"

	
*	foreach j in DT_score_pre DT_time_pre DT_per_min_pre ptsbl_overall_score ///
*	rosen_bl cpcs_bl {


	foreach s in mean se pv obs {
		matrix Att_`j'_post_`s' = J(1,3,.)
	}
	
	
	
	
	*** Reg of attririon status on treatment dummy
	 reg att_`j'_post treatment if `j'_pre != ., cluster(school_no)

		
	matrix Att_`j'_post_temp = r(table)
	matrix Att_`j'_post_r2 = e(r2)
	matrix Att_`j'_post_obs = e(N)

	 cgmwildboot_2 att_`j'_post treatment if `j'_pre != ., cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix Att_`j'_post_temp_p = e(Pvals)

	
	foreach s in mean be se pv rpv wpv {
		matrix Att_`j'_post_`s' = J(1,2,.)
	}

	foreach i in 1 2 {
		* beta
		matrix Att_`j'_post_mean[1,`i'] = Att_`j'_post_temp[1,`i']
		* standard error
		matrix Att_`j'_post_se[1,`i'] = Att_`j'_post_temp[2,`i']
		* p value
		matrix Att_`j'_post_pv[1,`i'] = Att_`j'_post_temp[4,`i']
		matrix Att_`j'_post_wpv[1,`i'] = Att_`j'_post_temp_p[`i',1]
	}

	
	

	
	
	
		** Reg of baseline-outcome on treatment for attritors
	
*		di ""
*		di ""
*		di ""
*		di "Cluster reg of baseline `j' with respect to the attrition status on endline `j'"
	 reg `j'_pre treatment if att_`j'_post == 1, cluster(school_no)

		
	matrix Att_`j'_pre_temp = r(table)
	matrix Att_`j'_pre_r2 = e(r2)
	matrix Att_`j'_pre_obs = e(N)

	 cgmwildboot_2 `j'_pre treatment if att_`j'_post == 1, cluster(school_no) bootcluster(school_no) null(0) reps(999) seed(1)
	matrix Att_`j'_pre_temp_p = e(Pvals)

	
	foreach s in mean be se pv rpv wpv {
		matrix Att_`j'_pre_`s' = J(1,2,.)
	}

	foreach i in 1 2 {
		* beta
		matrix Att_`j'_pre_mean[1,`i'] = Att_`j'_pre_temp[1,`i']
		* standard error
		matrix Att_`j'_pre_se[1,`i'] = Att_`j'_pre_temp[2,`i']
		* p value
		matrix Att_`j'_pre_pv[1,`i'] = Att_`j'_pre_temp[4,`i']
		matrix Att_`j'_pre_wpv[1,`i'] = Att_`j'_pre_temp_p[`i',1]
	}

	
}





***************      AttA1_`i'_pre_pv


foreach j in DT_score pts rosen cpcs {
	foreach p in pre post {
		forvalues item = 1/6 {
			forvalues scale = 1/1 {
				if Att_`j'_`p'_wpv[`item', `scale']<=0.01 {
					local star_`j'_`p'_`item'`scale' %3s "***"
				}
				else if (Att_`j'_`p'_wpv[`item', `scale']>0.01) & (Att_`j'_`p'_wpv[`item', `scale']<=0.05) {
					local star_`j'_`p'_`item'`scale' %2s "**"
				}
				else if (Att_`j'_`p'_wpv[`item', `scale']>0.05) & (Att_`j'_`p'_wpv[`item', `scale']<=0.10) {
					local star_`j'_`p'_`item'`scale' %1s "*"
				}
				else {
					local star_`j'_`p'_`item'`scale'  ""
				}
			} // forvalues `scale' loop

		}  // forvalues `item' loop
	}  // foreach `p' loop
} // forvalues `j' loop



foreach j in DT_score pts rosen cpcs {
foreach p in pre post {
forvalues item = 1/6 {
		forvalues scale = 2/2 {
		if Att_`j'_`p'_pv[`item', `scale']<=0.01 {
			local star_`j'_`p'_`item'`scale' %3s "***"
		}
		else if (Att_`j'_`p'_pv[`item', `scale']>0.01) & (Att_`j'_`p'_pv[`item', `scale']<=0.05) {
			local star_`j'_`p'_`item'`scale' %2s "**"
		}
		else if (Att_`j'_`p'_pv[`item', `scale']>0.05) & (Att_`j'_`p'_pv[`item', `scale']<=0.10) {
			local star_`j'_`p'_`item'`scale' %1s "*"
		}
		else {
			local star_`j'_`p'_`item'`scale'  ""
		}
	} // forvalues `scale' loop

}  // forvalues `item' loop
}  // foreach `p' loop


} // forvalues `j' loop





tempname hh
file open `hh' using "$pardir/tableJ1.tex", write replace
file write `hh' "" _newline
file write `hh' "% Date: `c(current_date)'" _newline
file write `hh' "% Time: `c(current_time)'" _newline
file write `hh' "" _newline


file write `hh' "\begin{table}[t!]\footnotesize" _newline
file write `hh' "  \centering" _newline
file write `hh' "  \caption{Attrition Status}" _newline
file write `hh' "\label{tab:attrition}" _newline
file write `hh' "\scalebox{0.85}{" _newline
file write `hh' "\begin{threeparttable}" _newline
file write `hh' "\begin{tabular}{lccc}\toprule" _newline




file write `hh' "\textbf{Panel A: Sample Attrition}\\"_newline
file write `hh' "\midrule"_newline
file write `hh' "Dependent Variable & \multicolumn{3}{c}{Attrition Status across Outcome Measures}  \\"_newline

file write `hh' "\cmidrule{2-4}             & DT Score   & PTSII-C Score\textsuperscript{b} & RSES/CPCS Index\textsuperscript{c}   \\" _newline
file write `hh'  "& (1) & (2) & (3)    \\\midrule\midrule" _newline
file write `hh' "Treatment           &"  %04.3f (Att_DT_score_post_mean[1,1]) `star_DT_score_post_11'   "&"  %04.3f (Att_pts_post_mean[1,1]) `star_pts_post_11'   "&"  %04.3f (Att_rosen_post_mean[1,1]) `star_rosen_post_11'    "\\" _newline
file write `hh' "         & (" %04.3f (Att_DT_score_post_se[1,1]) ")                     &(" %04.3f (Att_pts_post_se[1,1]) ") &(" %04.3f (Att_rosen_post_se[1,1]) ") \\" _newline
file write `hh' "Constant            &" %04.3f (Att_DT_score_post_mean[1,2]) `star_DT_score_post_12'  "& " %04.3f (Att_pts_post_mean[1,2]) `star_pts_post_12' "&" %04.3f (Att_rosen_post_mean[1,2]) `star_rosen_post_12' "\\" _newline
file write `hh' "      &(" %04.3f (Att_DT_score_post_se[1,2]) ")&(" %04.3f (Att_pts_post_se[1,2]) ")&(" %04.3f (Att_rosen_post_se[1,2]) ")\\" _newline
file write `hh' "       &                 &                         &                   \\\midrule" _newline
file write `hh' " Num of Obs.         &" (Att_DT_score_post_obs[1,1]) " &" (Att_pts_post_obs[1,1]) "&" (Att_rosen_post_obs[1,1]) "\\" _newline
file write `hh' "R-squared           &" %04.3f (Att_DT_score_post_r2[1,1]) "& " %04.3f (Att_pts_post_r2[1,1]) "& " %04.3f (Att_rosen_post_r2[1,1]) "\\\midrule" _newline
file write `hh' "\\" _newline
file write `hh' "\textbf{Panel B: Attrition Only Sample}\\" _newline
file write `hh' "\midrule" _newline 
file write `hh' "Dependent Variable && Baseline PTSII-C Score&\\\midrule\midrule" _newline
file write `hh' "Treatment           && "%04.3f (Att_pts_pre_mean[1,1]) `star_pts_pre_11'" &   \\" _newline
file write `hh' "                   &&  (" %04.3f (Att_pts_pre_se[1,1]) ")                 &  \\" _newline
file write `hh' "Constant            &&    "%04.3f (Att_pts_pre_mean[1,2]) `star_pts_pre_12'" & \\" _newline
file write `hh' "                   &&  (" %04.3f (Att_pts_pre_se[1,2]) ")                &   \\" _newline
file write `hh' "                     &&&         \\                                         \midrule" _newline
file write `hh' "Num of Obs.         &&   " (Att_pts_pre_obs[1,1]) "                     &      \\" _newline
file write `hh' "R-squared           &&    "%04.3f (Att_pts_pre_r2[1,1]) "               &      \\" _newline
file write `hh' "\\" _newline
file write `hh' "\bottomrule" _newline



file write `hh' "\end{tabular}" _newline
file write `hh' "\end{threeparttable}" _newline
file write `hh' "}" _newline
file write `hh' "\label{tab:addlabel}%" _newline
file write `hh' "\end{table}" _newline

file write `hh' "" _newline
file write `hh' "" _newline
file write `hh' "" _newline
file write `hh' "" _newline

file close `hh'



	
	
	
	
	



